//
//  StepViewController.m
//  FVQuickMessage
//
//  Created by dllo on 16/8/13.
//  Copyright © 2016年 zty. All rights reserved.
//

#import "StepViewController.h"

#import <CoreMotion/CoreMotion.h>
#import "QYPedometerManager.h"

@interface StepViewController ()

@property (nonatomic, retain) UILabel *stepCountLabel;

@property (nonatomic, retain) UILabel *distanceLabel;

@property (nonatomic, retain) CMPedometer *pedometer;

@property (nonatomic, retain) NSString *dis;

@property (nonatomic, retain) UILabel *upLabel;

@end

@implementation StepViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createSubViews];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    // Do any additional setup after loading the view.
}

-(void)createSubViews {
    
    self.navigationController.navigationBar.translucent = NO;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 2 + 50)];
    
    self.view.backgroundColor = [UIColor blueColor];
    
    [self.view addSubview:view];
    
    self.stepCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 40, [UIScreen mainScreen].bounds.size.width - 40)];
    
    self.stepCountLabel.center = view.center;
    
    self.stepCountLabel.backgroundColor = [UIColor whiteColor];
    
    self.stepCountLabel.layer.cornerRadius = self.stepCountLabel.bounds.size.height / 2;
    
    self.stepCountLabel.layer.masksToBounds = YES;
    
    self.stepCountLabel.layer.borderColor = [UIColor orangeColor].CGColor;
    
    self.stepCountLabel.layer.borderWidth = 5;
    
    self.stepCountLabel.text = @"0步";
    
    self.stepCountLabel.textColor = [UIColor blackColor];
    
    self.stepCountLabel.textAlignment = NSTextAlignmentCenter;
    
    self.stepCountLabel.font = [UIFont systemFontOfSize:50];
    
    [view addSubview:_stepCountLabel];
    
    UIImageView *distance = [[UIImageView alloc] initWithFrame:CGRectMake(20, view.bounds.size.height + 10, 32, 32)];
    
    distance.image = [UIImage imageNamed:@"跑"];
    
    [self.view addSubview:distance];
    
    self.distanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, view.bounds.size.height, [UIScreen mainScreen].bounds.size.width - 40, 50)];
    
    self.distanceLabel.backgroundColor = [UIColor whiteColor];
    
    self.distanceLabel.text = @"当前步行了0km";
    
    self.distanceLabel.font = [UIFont systemFontOfSize:20];
    
    self.distanceLabel.textColor = [UIColor blackColor];
    
    [self.view addSubview:_distanceLabel];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, view.bounds.size.height + _distanceLabel.bounds.size.height + 10, 32, 32)];
    
    imageView.image = [UIImage imageNamed:@"天眼"];
    
    [self.view addSubview:imageView];
    
    //    UILabel *start = [[UILabel alloc] initWithFrame:CGRectMake(70, view.bounds.size.height + _distanceLabel.bounds.size.height, [UIScreen mainScreen].bounds.size.width - 120, 50)];
    
    self.upLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, view.bounds.size.height + _distanceLabel.bounds.size.height, [UIScreen mainScreen].bounds.size.width - 120, 50)];
    
    self.upLabel.text = @"您今天爬了0层楼";
    
    self.upLabel.font = [UIFont systemFontOfSize:20];
    
    self.upLabel.textColor = [UIColor blackColor];
    
    [self.view addSubview:_upLabel];
    
    [self stepCalculation];
    
}

-(void)stepCalculation {
    
    if ([QYPedometerManager isStepCountingAvailable]) {
        [[QYPedometerManager shared]
         startPedometerUpdatesTodayWithHandler:^(QYPedometerData *pedometerData, NSError *error) {
             if (!error) {
                 self.stepCountLabel.text = [NSString stringWithFormat:@"%@步", pedometerData.numberOfSteps];
                 
                 self.distanceLabel.text = [NSString stringWithFormat:@"当前步行了%@距离", pedometerData.distance];
                 
                 self.upLabel.text = [NSString stringWithFormat:@"您今天爬了%@层楼", pedometerData.floorsAscended];
             }
         }];
    } else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"此设备不支持计步功能" message:@"仅支持iPhone5s及其以上" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction:action];
        
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
